-- artStatue
-- Created by panyl Dec/29/2016
-- 艺术雕像

function initArtStatue(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local statue = self.gridData.statue;
    local name = ArtStatueM.query(statue, "name");

    local pos = self.gridData:getPos();

    redrawArtStatue(self);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("button_spell");

            -- 如果还未完成
            if not ArtStatueM.is_completed(pos) then
                local function carving()
                    local ret = ArtStatueM.can_carving_statue(pos);
                    if true ~= ret then
                        if type(ret) == "string" then
                            alert(getLocStr(ret));
                        end
                    else
                        DungeonActionM.go("carving_statue", pos);

                        -- 如果还未完成
                        if not ArtStatueM.is_completed(pos) then
                            alert(string.format(getLocStr("continue_carving"), name));

                            -- 播放制作特效
                            playEffect(self, 1357, 25, -20, nil, 0.9);
                        else
                            -- 已完成
                            alert(string.format(getLocStr("complete_carving"), name));
                            playEffect(self, 1261, -18, 140, nil, 1.3);
                            playEffect(self, 1262, -20, 120, nil, 1.3);
                            -- 重绘下
                            initArtStatue(self);
                        end
                    end

                    EventMgr.fire(event.PLAYER_MOVE, pos);
                end

                local left_times = ArtStatueM.calc_max_engrave(statue) - tonumber(self.gridData.times);
                local dialog = string.format(buildingInfo["dialog2"], name, left_times);

                self:openBuildingView2(carving, nil, buildingInfo["view_text"], getLocStr("btn_text_leave"), dialog);
                return;
            else
                -- 已经完成的
                local function touch()
                    DungeonActionM.go("take_art_statue_bonus", pos);

                    EventMgr.fire(event.PLAYER_MOVE, pos);
                end

                local desc = extractString(ArtStatueM.get_bonus_desc());
                desc = string.format(buildingInfo["dialog"], "[blue]" .. desc .. "[-]");
                self:openBuildingView(touch, -20, desc, buildingInfo["view_text2"], name, self.iconPath);
            end
        end
    end
    self.icon:addTouchEventListener(onClicked);
end

function redrawArtStatue(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local statue = self.gridData.statue;
    local name = ArtStatueM.query(statue, "name");

    local pos = self.gridData:getPos();

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    -- 如果已经完成了
    if ArtStatueM.is_completed(pos) then
        iconPath = getLevelItemIconPath(ArtStatueM.query(statue, "icon"));
    end

    self:cleanItem();

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:setVisible(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.hasShadow = false;

    -- 如果已经完成了
    if ArtStatueM.is_completed(pos) then
        -- 闪光
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end

    self:initGridState();
end
